package com.qf.sy2103.ssm.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/g")
@Slf4j
public class GeneralController {
//    http://localhost:8080/g/t
    @RequestMapping("/t")
    public void analyseRequest(HttpServletRequest request){

        // 请求行
        final String uri = request.getRequestURI();
        log.debug("uri:{}",uri);

        log.debug("请求头部分 ====================================");
        // 请求头
        final Enumeration<String> headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            final String headerName = headerNames.nextElement();
            final String headerValue = request.getHeader(headerName);
            log.debug("{} ---> {}",headerName,headerValue);
        }


        log.debug("请求参数部分 ====================================");
        // 请求当中的参数获取
        final Map<String, String[]> parameterMap = request.getParameterMap();
        final Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            final String key = entry.getKey();
            final String[] value = entry.getValue();
            log.debug("{} ---> {}",key,value);
        }

        log.debug("文件上传部分 ====================================");
        // 图片上传类的请求  Multipart类型的请求
        DefaultMultipartHttpServletRequest req = (DefaultMultipartHttpServletRequest) request;
        final MultiValueMap<String, MultipartFile> multiFileMap = req.getMultiFileMap();
        final Set<Map.Entry<String, List<MultipartFile>>> entries1 = multiFileMap.entrySet();
        for (Map.Entry<String, List<MultipartFile>> entry : entries1) {
            final String key = entry.getKey();
            final List<MultipartFile> value = entry.getValue();
            log.debug("{} ---> {}",key,value);
        }


    }

}
